//
//  YXPlayerRequestManger.swift
//  swift_wobo
//
//  Created by yuxin on 2018/12/2.
//  Copyright © 2018 wuxiantianli. All rights reserved.
//

import UIKit
import AVFoundation

protocol YXPlayerRequestMangerDelegate: class {
    
}

class YXPlayerRequestManger: NSObject {

    var loadingRequest: AVAssetResourceLoadingRequest?
    
    ///请求起始位置
    var requestOffset: Int64 = 0
    ///缓存、冲长度
    var cacheLength: Int64 = 0
    ///文件长度
    var fileLength: Int64 = 0
    
    private var requestList = [AVAssetResourceLoadingRequest]()

    
//    weak var delegate: YXPlayerRequestMangerDelegate?
    

    func add(loadingRequest request: AVAssetResourceLoadingRequest){
        for item in requestList {
            item.finishLoading()
        }
        requestList.removeAll()
        
        requestList.append(request)
    }
    
    func currentRange() -> NSRange {
        let range = NSRange.init(location: Int(requestOffset), length: Int(fileLength))
        return range
    }
    
    func cancel() {
        for item in requestList {
            item.finishLoading()
        }
        
        requestList.removeAll()
    }
 
    func remove(loadingRequest request: AVAssetResourceLoadingRequest){
        request.finishLoading()
        requestList.removeAll { (item) -> Bool in
            return item == request
        }
    }
    
}

